<template>
  <el-dialog title="新建职位" :visible.sync="formVisible">
    <el-form ref="dataForm" :rules="formRules" :model="form"
             label-position="left" size="small"
             label-width="100px" style="width: 400px; margin-left:50px;">
      <el-form-item label="职位名称" prop="name">
        <el-input v-model="form.name"/>
      </el-form-item>
      <el-form-item label="职位备注" prop="note">
        <el-input v-model="form.note"/>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="formVisible = false">
        取消
      </el-button>
      <el-button type="primary"
                 @click="doCreate()">
        确认
      </el-button>
    </div>
  </el-dialog>
</template>

<script>
import positionApi from '@/api/position/position'

function initFormBean() {
  const formBean = {
    name: null,
    note: null
  }
  return formBean
}

export default {
  name: 'PositionAdd',
  data() {
    return {
      form: initFormBean(),
      formVisible: false,
      formRules: {
        name: [
          { max: 32, message: '长度不能超过32个字符', trigger: 'blur' }
        ],
        note: [
          { max: 255, message: '长度不能超过255个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    /**
     * 重置表单
     */
    resetForm() {
      this.form = initFormBean()
    },
    /**
     * 打开新建表单
     */
    handleCreate() {
      this.resetForm()
      this.formVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    /**
     * 执行新建操作
     */
    doCreate() {
      this.$refs['dataForm'].validate()
        .then(() => positionApi.create(this.form))
        .then(data => {
          this.formVisible = false
          this.$common.showMsg('success', '创建成功')
          this.$emit('created', data)
        })
    }
  }
}
</script>
